/*****************************************************************************//*!
* file  : test.c
* creat : apleilx
* data  : 2017/03/02
*******************************************************************************/
#define   osObjectsCreat                                        // define objects in rtxapp module
#include "os_obj.h"                                          // RTOS object definitions
#include "cmsis_os.h"

osThreadDef(LedCtr, LedCtrTask, 24, 0, 1024);
osThreadDef(Modbus, modbus_task, 12, 0, 1024);
osThreadDef(Storage, StorageTaskEntry, 28, 0, 4096);
osThreadDef(IdleTask, StartDefaultTask, 30, 0, 1024);
osThreadDef(console, task_console, APP_CFG_TASK_CONSOLE_PRIO, 0, 1024);
osThreadDef(system, task_system, APP_CFG_TASK_SYSTEM_PRIO, 0, 1024);
osThreadDef(tcan, task_can, 22, 0, 1024);



void os_mem_pool_creat(void);
/*----------------------------------------------------------------------------
 *      Thread 1 'Thread_Name': Sample thread
 *---------------------------------------------------------------------------*/
int os_thread_init(void)
{
  return 0;
}

/*******************************************************************************
* @brief  os_thread_creat.
* \param[in] none
* \retval: res
*******************************************************************************/
int os_thread_creat(void)
{
  dbg_puts("\n\n");
  dbg_puts("=============================================================\n");
  dbg_puts("=========================thread creat========================\n");
  dbg_puts("=============================================================\n");
  
  os_obj_creat();

  osThreadCreate(osThread(LedCtr), NULL);

  osThreadCreate(osThread(Modbus), NULL);
  
  osThreadCreate(osThread(Storage), NULL);  

  osThreadCreate(osThread(IdleTask), NULL);
  
  osThreadCreate(osThread(tcan), NULL);
  
  os_obj.tid_console = osThreadCreate(osThread(console), NULL);

  os_obj.tid_system = osThreadCreate(osThread(system), NULL);

  return 0;
}

/*******************************************************************************
* @brief  os_obj_creat.
* \param[in] none
* \retval: none
*******************************************************************************/
int os_obj_creat(void)
{
  UINT os_err;

  
  /* creat mutex */
  tx_mutex_create(&os_obj.mutex_printf,
    "mutex_printf",
    TX_NO_INHERIT);  //no prio 
  os_obj.mid_printf = &os_obj.mutex_printf;

  tx_mutex_create(&os_obj.mutex_common,
    "mutex_common",
    TX_NO_INHERIT);  //no prio 
  os_obj.mid_common = &os_obj.mutex_common;

  /* creat quene */
  tx_queue_create(&os_obj.quene_tst,          //Pointer to queue control block
    "tst quene",                //Pointer to queue name
    1,                          //Size of each queue message (Number of ULONG)
    os_obj.quene_tst_mem,       //Starting address of the queue area
    sizeof(os_obj.quene_tst_mem));      //Number of bytes in the queue
  os_obj.qid_tst = &os_obj.quene_tst;

  /* creat event */
  tx_event_flags_create(&os_obj.event_tst,
    "event tst");
  os_obj.eid_tst = &os_obj.event_tst;

  tx_event_flags_create(&os_obj.event_mb,
    "event mb");
  os_obj.eid_mb = &os_obj.event_mb;

  /* creat semaphore */
  tx_semaphore_create(&os_obj.sem_tst,
    "sem tst",
    1);              //init val  
  os_obj.sid_tst = &os_obj.sem_tst;

  // ccm poll creat
	os_mem_pool_creat();
  
  (void)os_err;
  
  os_api_exe_check();
  
  return(0);
}


/*******************************************************************************
* @brief  tx_application_define : called by tx_kernel_enter.
* \param[in] first_unused_memory :mem address that is unused
* \retval: none
*******************************************************************************/
void  tx_application_define(void *first_unused_memory)
{
  /* Create the thread(s) */
  /* definition and creation of IdleTask */
  
  os_thread_creat();
}
